//
//  QXBannerAdView.m
//  iAccountBook
//
//  Created by 侯伟 on 2021/9/5.
//

#import "QXBannerAdView.h"
#import "QXAdvert.h"
#import "QXAdHandler.h"

@interface QXBannerAdView ()

@property (nonatomic,strong)UIImageView *adImageView;
@property (nonatomic,strong)UIButton* closeBtn;
@property (nonatomic,strong)QXAdvert* advert;

@end

@implementation QXBannerAdView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViews];
    }
    return self;
}

- (void)setupViews{
    
    // 1.广告图片
    UIImageView *adImageView = [[UIImageView alloc] initWithFrame:self.bounds];
    adImageView.contentMode = UIViewContentModeScaleAspectFill;
    adImageView.clipsToBounds = YES;
    adImageView.userInteractionEnabled = YES;
    [self addSubview:adImageView];
    _adImageView = adImageView;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];
    [adImageView addGestureRecognizer:tap];

    // 2.关闭按钮
    
    NSBundle* bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"QXAdSDK" ofType:@"bundle"]];
    NSString* path = [bundle pathForResource:@"close" ofType:@"png"];
    UIImage* image = [UIImage imageWithContentsOfFile:path];
    CGFloat btnW = 16;
    UIButton * closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.bounds.size.width- btnW-4, 4, btnW, btnW)];
    [closeBtn addTarget:self action:@selector(closeClick:) forControlEvents:UIControlEventTouchUpInside];
    [closeBtn setImage:image forState:UIControlStateNormal];
    [self addSubview:closeBtn];
    _closeBtn = closeBtn;
}


- (void)loadAdData{
    [[QXAdHandler shareHander] requestQXAdWithAdType:ADType_Banner block:^(QXAdvert * _Nullable ad) {
        if(!ad){
            if(self.delegate && [self.delegate respondsToSelector:@selector(QXBannerAdLoadFaild:)]){
                [self.delegate QXBannerAdLoadFaild:self];
            }
            return;
        }
        self.advert = ad;
        
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSURL* URL = [NSURL URLWithString:ad.image];
            NSData* imageData = [NSData dataWithContentsOfURL:URL];
            UIImage* image = [UIImage imageWithData:imageData];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                self.adImageView.image = image;
                
                if(image){
                    if(self.delegate && [self.delegate respondsToSelector:@selector(QXBannerAdDidLoaded:)]){
                        [self.delegate QXBannerAdDidLoaded:self];
                    }
                }else{
                    if(self.delegate && [self.delegate respondsToSelector:@selector(QXBannerAdLoadFaild:)]){
                        [self.delegate QXBannerAdLoadFaild:self];
                    }
                }
            });
        });
    }];
}

#pragma mark -- 点击
- (void)click:(UITapGestureRecognizer*)tap{
    
    NSURL *url = [NSURL URLWithString:self.advert.url];
    if(url){
        if (@available(iOS 10.0, *)) {
            [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {}];
        } else {
            [[UIApplication sharedApplication] openURL:url];
        }
    }
    
    [[QXAdHandler shareHander] selfAdClickCallBack:self.advert.click_url];
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXBannerAdDidClick:)]){
        [self.delegate QXBannerAdDidClick:self];
    }
}

- (void)closeClick:(UIButton*)sender{
    [self removeFromSuperview];
}


- (void)didMoveToSuperview{
    [super didMoveToSuperview];
    if(self.superview){
        if(self.delegate && [self.delegate respondsToSelector:@selector(QXBannerAdDidDisplay:)]){
            [self.delegate QXBannerAdDidDisplay:self];
        }
        [[QXAdHandler shareHander] selfAdShowCallBack:self.advert.show_url];
    }else{
        if(self.delegate && [self.delegate respondsToSelector:@selector(QXBannerAdDidClosed:)]){
            [self.delegate QXBannerAdDidClosed:self];
        }
    }
}


@end
